<template>
	<view>
		<!-- 低功耗蓝牙组件 -->
		<bluetoothAdapterCore ref="bluetoothAdapterCore" :bleInfo.sync="BLEInformation"
			@deviceListChange="deviceListChange" :showLog="showLog">
		</bluetoothAdapterCore>
		<!-- 打印机标签组件 -->
		<printerTable ref="printerTable" :bleInfo.sync="BLEInformation" :showLog="showLog">
		</printerTable>

	</view>
</template>

<script>
	// 引用连接蓝牙组件
	import bluetoothAdapterCore from './common/bluetooth_adapter_core.vue'
	// 引用打印标签组件
	import printerTable from './common/printer_table/printer_table.vue';

	let SUCCESS = {
		code: 0
	}
	let ERROR = function error(msg) {
		return {
			code: 1,
			msg: msg
		}
	}
	//历史连接信息key
	const HIS_CONNECT_DEVICE_KEY = "HIS_CONNECT_DEVICE_KEY";

	export default {
		components: {
			bluetoothAdapterCore,
			printerTable
		},
		data() {
			return {
				deviceList: [], //蓝牙设备列表
				//蓝牙核心连接信息
				BLEInformation: {
					deviceState: 0, //连接状态，1.已连接，0.未连接
					deviceName: "", //当前连接的蓝牙名称
					deviceId: "", //当前连接的蓝牙地址
					writeCharaterId: "",
					writeServiceId: "",
					notifyCharaterId: "",
					notifyServiceId: "",
					readCharaterId: "",
					readServiceId: "",
					platform: "", //mp,android，ios
				},
			};
		},
		props: {
			//是否显示调试log
			showLog: {
				type: Boolean,
				default: false
			},
		},
		created() {
			//初始化
		},
		methods: {

			// ------------------------------主动调用
			//搜索设备
			startSearch(fun) {
				this.$refs.bluetoothAdapterCore.startSearch(fun);
			},
			//连接设备
			connectPrinter(device, fun) {
				this.$refs.bluetoothAdapterCore.connect(device, (res) => {
					fun(res);
					//保存连接信息到历史记录
					try {
						uni.setStorageSync(HIS_CONNECT_DEVICE_KEY, device);
					} catch (error) {
						console.log(error)
					}
				});
			},
			//自动连接打印机
			async autoConnectPrinter(fun) {
				let device = null; //要连接的打印机
				// 搜索蓝牙列表
				let res = await new Promise(async (resolve) => {
					let that = this;
					this.startSearch((res) => {
						if (res.code == 0) {
							// 保存的历史连接蓝牙地址
							try {
								device = uni.getStorageSync(HIS_CONNECT_DEVICE_KEY);
							} catch (error) {
								console.log(error)
							}
							if (!device) {
								// 查找名称以 'GP-' 开头的蓝牙设备
								device = that.deviceList.find(function(device) {
									return device.name.indexOf('GP-') === 0;
								});
							}
						}
						resolve(res);
					});
				});

				if (res.code != 0) {
					fun(res);
					return;
				}

				if (device) { //有符合条件的蓝牙设备
					// 尝试连接蓝牙
					this.connectPrinter(device, (res) => {
						fun(res, device);
					});
				} else {
					fun(ERROR("未找到符合条件的打印机"))
				}
			},
			// -------------------------------自动回调
			//设备列表
			deviceListChange(deviceList) {
				this.deviceList = deviceList;
				this.$emit('deviceListChange', deviceList);
			},

			// -------------------------------打印标签
			//打印标签（自动队列）
			sendBleLabel(tableValue, fun) {
				this.$refs.printerTable.sendBleLabel(tableValue, fun);
			},
			/**			
			 * 打印标签测试页（自动队列）
			 * @param {Object} fun
			 * @param {Object} templateId 支持指定模板测试 所有值为 XXXX
			 */
			sendBleLabelTest(fun, templateId) {
				this.$refs.printerTable.sendBleLabelTest(fun, templateId);
			},
			//打印标签自检页（自动队列）
			sendBleLabelSelfTest(fun) {
				this.$refs.printerTable.sendBleLabelSelfTest(fun);
			},
			//打印机配置（自动队列）
			setPrinterConfig(printerConfig, fun) {
				this.$refs.printerTable.setPrinterConfig(printerConfig, fun);
			},


		}
	}
</script>

<style scoped>

</style>
<style lang="scss">

</style>